<?php

namespace mbilling\BillingBundle\Model;

use Doctrine\ORM\EntityManager;
use mbilling\BillingBundle\Entity\domainsDns;
use mbilling\BillingBundle\Entity\domains;

Class DomainsDnsModel
{
    protected $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }
    
    public function findAll()
    {
        $dnss = $this->em->getRepository('BillingBundle:domainsDns')->findAll();
        return $dnss;
    }
    
    public function FindByPK($dns_id)
    {
        $dns = $this->em->find('BillingBundle:domainsDns', $dns_id);
        return $dns;
    }

    public function findByDomainId($domain_id)
    {
        $dns = $this->em->getRepository('BillingBundle:domainsDns')->findBy(array('domain_id' => $domain_id));
        return $dns;
    }
    
    public function findByHash($hash)
    {
        $dnss = $this->em->getRepository('BillingBundle:domainsDns')->findAll();
        foreach ($dnss as $dns)
        {
            $domain = $this->em->getRepository('BillingBundle:domains')->findOneBy(array('id' => $dns->getDomainId()));
            if ( md5( $dns->getName().$dns->getType().$dns->getValue().$domain->getName() ) == $hash )
                return $dns;
        }
        return NULL;
    }
    
    public function findAllNotActive()
    {
        $dnss = $this->em->getRepository('BillingBundle:domainsDns')->findBy(array('is_active' => "0"));
        return $dnss;
    }
    
    public function create($oEntity)
    {    
        $this->em->persist($oEntity);
        $this->em->flush();
    }
    
    public function removeByPK($dns_id)
    {
        $dns = $this->em->find('BillingBundle:domainsDns', $dns_id);
        $this->em->remove($dns);
        $this->em->flush();
    }
}
?>
